<?php
class Site_model extends CI_Model {

	
	function get_entries(){
		
		$query = $this->db->query('SELECT * FROM entry, mood, category WHERE entry.moodID = mood.moodID AND category.categoryID = entry.categoryID ORDER BY date DESC');

		foreach ($query->result() as $row)
		{
		    $data[] = $row;
		}
		
		return $data;
	}

	function get_entry($id){

		$sql = "SELECT * FROM entry, mood, category WHERE entry.moodID = mood.moodID AND category.categoryID = entry.categoryID AND entryID = ?";

		$query = $this->db->query($sql, $id);

		foreach ($query->result() as $row)
		{
		    $data[] = $row;
		}
		return $data;
	}

	function add_entry(){

		$today = date("Y-m-d");

		$data = array(
			'headline' => $this->input->post('headline'),
			'text' => $this->input->post('text'),
			'author' => $this->input->post('author'),
			'date' => $today,
			'moodID' => $this->input->post('mood'),
			'categoryID' => $this->input->post('category')	
		);

		$insert = $this->db->insert('entry', $data);
		return $insert;
	}

	function add_entry_image($image_data){

		$today = date("Y-m-d");
		$data = array(
		'headline' => $this->input->post('headline'),
		'author' => $this->input->post('author'),
		'date' => $today,
		'moodID' => $this->input->post('mood'),
		'categoryID' => $this->input->post('category'),
		'imageURL' => $image_data['file_name'],
		'isImageEntry' => 'true'
		);
		$insert = $this->db->insert('entry', $data);
		return $insert;
	}

	function search_entries($terms){
        $sql = "SELECT * FROM entry, mood, category WHERE entry.moodID = mood.moodID AND category.categoryID = entry.categoryID AND MATCH (headline, text, author) AGAINST (?)";
        $query = $this->db->query($sql, $terms);

        $data = array();
        foreach ($query->result() as $row)
		{
		    $data[] = $row;
		}
		return $data;
	}

	function get_moods(){
		$query = $this->db->get('mood');

		foreach ($query->result() as $row)
		{
		    $data[] = $row;
		}	
		return $data;
	}

	function get_categories(){
		$query = $this->db->get('category');

		foreach ($query->result() as $row)
		{
		    $data[] = $row;
		}	
		return $data;	
	}

	function update_entry($id){
		$today = date("Y-m-d");
		$data = array(
		'headline' => $this->input->post('headline'),
		'text' => $this->input->post('text'),
		'author' => $this->input->post('author'),
		'date' => $today,
		'moodID' => $this->input->post('mood'),
		'categoryID' => $this->input->post('category')
		);
		$this->db->where('entryID', $id);
		$this->db->update('entry', $data);
		return true;
	}

	function get_xml(){
		
		$this->load->dbutil();		
		$query = $this->db->query('SELECT * FROM entry');
		
			$config = array (
                  'root'    => 'root',
                  'element' => 'entry',
                  'newline' => "\n",
                  'tab'    => "\t"
                );

		$data = '<!DOCTYPE entry SYSTEM "'. base_url() .'xml/entry.dtd">';
		$data = $data . $this->dbutil->xml_from_result($query, $config); 
	 	$data = utf8_encode($data);
//if (write_file('./xml/export.xml', $data))
		{
			//$file = file_get_contents('./xml/export.xml'); // Read the file's contents
			$name = 'export.xml';
			force_download($name, $data);
			
			return true;
		}
		//else {return false;}
	}
}